import javax.swing.JOptionPane;

/**
 * 
 *is the control class of update book Gui.
 */

public class UpDateBookControl 
{
	/** array of strings */
	public String[] upDateData;
	
	/** 
	 * this method collect all the new book details and pass it forward.
	 * 
	 * @return void
	 */
	public void upDate()
	{
		
		String temp = new String();
		upDateData = new String[13];
		upDateData[0]=Main.gusers.id;
		upDateData[1]="update_details";
		upDateData[2]=Main.welcomeScreen.res[0];
		upDateData[3]=Main.upDateBookG.getJTitleField().getText();
		upDateData[4]=Main.upDateBookG.getJAuthorField().getText();	
		upDateData[5]=Main.upDateBookG.getJLanguageField().getText();	
		upDateData[6]=Main.upDateBookG.getJKeyWordsField().getText();	
		upDateData[7]=Main.upDateBookG.Summery.getText();
		upDateData[8]=Main.upDateBookG.getJIndexField().getText();
		if(Main.upDateBookG.jCheckPdf.isSelected())
			upDateData[9]="yes";
		else
			upDateData[9]="no";
		if(Main.upDateBookG.jCheckDoc.isSelected())
			upDateData[10]="yes";
		else
			upDateData[10]="no";
		if(Main.upDateBookG.jCheckFb2.isSelected())
			upDateData[11]="yes";
		else
			upDateData[11]="no";
		upDateData[12]=Main.upDateBookG.jPrice1Field.getText();

		if(upDateData[2].equals("") || upDateData[3].equals("") || upDateData[4].equals("") || upDateData[5].equals("") || 
				upDateData[6].equals("") || upDateData[7].equals("") || upDateData[8].equals("") || upDateData[9].equals("") ||
				(upDateData[10].equals("no") && upDateData[11].equals("no") && upDateData[12].equals("no")))
		{
			 JOptionPane.showMessageDialog(null,"One or more fields are emppty !","ERROR",JOptionPane.ERROR_MESSAGE);
			 return;
		}
		Main.log.connection.accept("update");
		while(Main.log.flag==false);
		Main.log.flag=false;
		temp=ChatClient.getRs();
		if (temp.equals("error"))
			JOptionPane.showMessageDialog(null,"The Book was not UpDated,please try againe","ERROR",JOptionPane.INFORMATION_MESSAGE);
		else
		{
			JOptionPane.showMessageDialog(null,"The Book was UpDated","INFO",JOptionPane.INFORMATION_MESSAGE);
			if(JOptionPane.showConfirmDialog(null, "Do you wish to attach more Subjects or fields to this book ?","Confirmation", 0) == 0)
			{
					//upDateData[2]					
					Main.upDateBookG.setEnabled(false);
					Main.upDateBookG.setVisible(false);
					Main.searchBookG = new SearchBookGUI();
					Main.searchBookC = new SearchBookCONTROL();
					Main.log.frame.setSize(530,340);
					Main.log.frame.add(Main.searchBookG);
					Main.UpdBooksSubjectCTRL =new UpdateBookSubjectCTRL();
					Main.UpdBooksSubjectGui =new UpdateBookSubject(upDateData[2]);
					//Main.UpdBooksSubjectGui =new UpdateBookSubject("1000");
					Main.log.frame.add(Main.UpdBooksSubjectGui.panel);			
					Main.UpdBooksSubjectGui.panel.setEnabled(true);
					Main.UpdBooksSubjectGui.panel.setVisible(true);
					return;
				
			}
		}
		Main.upDateBookG.setEnabled(false);
		Main.upDateBookG.setVisible(false);
		Main.searchBookG = new SearchBookGUI();
		Main.searchBookC = new SearchBookCONTROL();
		Main.log.frame.setSize(730,560);
		Main.log.frame.add(Main.searchBookG);
		Main.searchBookG.setEnabled(true);
		Main.searchBookG.setVisible(true);
		return;
	}

}
